package huaweiod.first;

import java.util.PriorityQueue;

/**
 * 华为机试真题 Java实现【最短木板长度】【100%通过率】【2022.11 Q4 新题】
 */
public class A_2023_31_MinBoardLength {

    public static int minBoardLength(int[] board, int len) {
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int i = 0; i < board.length; i++) {
            heap.add(board[i]);
        }
        while (len > 0) {
            if (!heap.isEmpty()) {
                heap.add(heap.poll() + 1);
            }
            len--;
        }
        return heap.isEmpty() ? 0 : heap.poll();
    }

    public static void main(String[] args) {
        int[] board = {4, 5, 3, 5, 5};
        System.out.println(minBoardLength(board, 3));        // 5
        int[] board1 = {4, 5, 3, 5, 5};
        System.out.println(minBoardLength(board1, 2));       // 4
    }
}
